/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDelta;
import com.velocityme.entity.Node;
import com.velocityme.entity.Role;
import com.velocityme.entity.User;
import com.velocityme.entity.UserAccess;
import com.velocityme.enums.ChangeDeltaItemType;
import com.velocityme.enums.ChangeDeltaType;
import com.velocityme.enums.Permission;
import com.velocityme.utility.PermissionCache;
import java.util.HashSet;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author rcrida
 */
@Stateless
public class UserAccessBean implements UserAccessLocal {

    @PersistenceContext
    private EntityManager em;

    public UserAccess createUserAccess(KeyLocal key, Long nodeId, Long userId, Long roleId) {
        Node node = em.find(Node.class, nodeId);
        key.assertHasPermission(node, Permission.USER_ACCESS_CREATE);
        User user = em.find(User.class, userId);
        Role role = em.find(Role.class, roleId);
        UserAccess userAccess = new UserAccess(node, user, role);
        // clear the permission cache for this user
        PermissionCache.getInstance().clearUser(user.getId());
        ChangeDelta changeDelta = node.addChangeDelta(ChangeDeltaType.CREATE_USER_ACCESS, key.getUser());
        changeDelta.addItem(ChangeDeltaItemType.NODE, node.getPathName());
        changeDelta.addItem(ChangeDeltaItemType.ROLE, role.toString());
        em.persist(userAccess);
        em.merge(user);
        em.merge(node);
        em.persist(changeDelta);
        return userAccess;
    }

    public void deleteUserAccess(KeyLocal key, Long userAccessId) {
        UserAccess userAccess = em.find(UserAccess.class, userAccessId);
        Node node = userAccess.getNode();
        key.assertHasPermission(node, Permission.USER_ACCESS_DELETE);
        ChangeDelta changeDelta = node.addChangeDelta(ChangeDeltaType.DELETE_USER_ACCESS, key.getUser());
        changeDelta.addItem(ChangeDeltaItemType.NODE, node.getPathName());
        changeDelta.addItem(ChangeDeltaItemType.ROLE, userAccess.getRole().toString());
        // clear the permission cache for this user
        User user = userAccess.getUser();
        PermissionCache.getInstance().clearUser(user.getId());
        em.persist(changeDelta);
        em.remove(userAccess);
    }

    @Override
    public Set<UserAccess> getUserAccessByNode(KeyLocal key, Node node) {
        key.validateKey();
        Set<UserAccess> accesses = new HashSet<UserAccess>();
        while(node != null) {
            accesses.addAll(em.createNamedQuery("findUserAccessByNode").setParameter("node", node).getResultList());
            node = node.getParentNode();
        }
        return accesses;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
 
}
